﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using YunHan.BuShi123.BLL;
using YunHan.BuShi123.Model;

public partial class Login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
        Session["MemberID"] = null;
        Session["MemberName"] = null;
        tbxUserName.Focus();
        if (!IsPostBack)
        {
            btnReset.OnClientClick = Window1.GetResetReference();
        }
    }

    protected void btnLogin_Click(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(tbxUserName.Text.Trim()))
        {
            FineUI.Alert.Show(" 用户名不能为空！", FineUI.MessageBoxIcon.Question);
        }

        if (tbxUserName.Text.Trim().Length > 20)
        {
            FineUI.Alert.Show("用户名字符数不能超过20个字符！", FineUI.MessageBoxIcon.Question);
        }

        if (string.IsNullOrEmpty(tbxPassword.Text.Trim()))
        {
            FineUI.Alert.Show(" 密码不能为空！", FineUI.MessageBoxIcon.Question);
        }

        if (tbxPassword.Text.Trim().Length > 16 || tbxPassword.Text.Trim().Length < 6)
        {
            FineUI.Alert.Show("密码字符数超出6~16个字符有效范围！", FineUI.MessageBoxIcon.Question);
        }

        string userName = tbxUserName.Text.Trim();
        string passWord = tbxPassword.Text.Trim();
        Admin_Model result = Admin_BLL.GetModel("UserName = @UserName AND PassWord = @PassWord"
            , new System.Data.SqlClient.SqlParameter("@UserName", userName)
            , new System.Data.SqlClient.SqlParameter("@PassWord", passWord));

        if (result != null)
        {
            Session["MemberID"] = result.ID;
            Session["MemberName"] = result.UserName;
            FineUI.PageContext.Redirect("/admin/index.aspx");
        }
        else
        {
            FineUI.Alert.ShowInTop("用户名或密码不正确！");
            return;
        }
    }



    #region MD5加密
    /// <summary>
    /// 用md5加密
    /// </summary>
    /// <param name="Sourcein">输入的未加密之前的字符串</param>
    /// <returns>加密后的字符串</returns>
    public static string EncodingToMD5(string Sourcein)
    {
        System.Security.Cryptography.MD5CryptoServiceProvider MD5CSP = new System.Security.Cryptography.MD5CryptoServiceProvider();
        byte[] MD5Source = System.Text.Encoding.UTF8.GetBytes(Sourcein);
        byte[] MD5Out = MD5CSP.ComputeHash(MD5Source);
        return Convert.ToBase64String(MD5Out);
    }
    #endregion

    #region 返回MD5加密数据
    public string md5(string str)
    {
        return FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5");
    }
    #endregion

}